package com.evan.kdl.test.day01;

import java.util.concurrent.CompletableFuture;

/**
 * @author huangzhongliang
 * exceptionally 用于处理回调链上的异常，回调链上出现的任何异常，回调链不继续向下执行，都在exceptionally中处理异常。
 * 因为exceptionally只处理一次异常，所以常常用在回调链的末端。
 * @time 2024/01/05 15:46
 */
public class ExceptionallyDemo {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            //int r = 1 / 0;
            return "result1";
        }).thenApply((result) -> {
            String str = null;
            int len = str.length();
            return "result2";
        }).thenApply(result -> "result3").exceptionally(ex -> {
            System.out.println("出现异常:" + ex.getMessage());
            return "Unknown";
        });
        String ret = future.join();
        System.out.println("ret = " + ret);
    }
}
